<?php
class TRF_Controller_Dispatcher extends Zend_Controller_Dispatcher_Standard
{
	static $_isChangeDispatch = false;

	public function dispatch( Zend_Controller_Request_Abstract $request, Zend_Controller_Response_Abstract $response )
	{
		if ( false == self::$_isChangeDispatch )
		{
			$this->_changeDispatch( $request, $response );
			self::$_isChangeDispatch = true;
		}

		parent::dispatch( $request, $response );
	}

	private function _changeDispatch( Zend_Controller_Request_Abstract & $request, Zend_Controller_Response_Abstract $response )
	{
		$module = $request->getParam( $request->getModuleKey() );
		$controller = $request->getParam( $request->getControllerKey() );
		$action = $request->getParam( $request->getActionKey() );

		$submodule = '';
		$subcontroller = '';
		$subaction = '';

		if ( 'admin' != $module )
		{
			$submodule = $module;
			$subcontroller = ( $controller ) ? $controller : 'index';
			$subaction = ( $action ) ? $action : 'index';

			$controller = 'index';
			$action = 'subroute';

			$module = 'default';
		}
		else
		{
			$submodule = $controller;
			$subcontroller = ( $action ) ? $action : 'index';
			//долно быть настроено в route
			//$subaction = ( $action ) ? $action : 'index';

			$controller = 'index';
			$action = 'subroute';

		}

		$request->setParam( $request->getModuleKey(), $module );
		$request->setModuleName( $module );
		$request->setParam( $request->getControllerKey(), $controller );
		$request->setControllerName( $controller );
		$request->setParam( $request->getActionKey(), $action );
		$request->setActionName( $action );

		$request->setParam( 'submodule', $submodule );
		$request->setParam( 'subcontroller', $subcontroller );
		$request->setParam( 'subaction', $subaction );
	}
}
